#include "stdafx.h"
#include "itemdb.h"
#include "sderrcode.h"
#include "role.h"
#include "rolemgr.h"
#include "mailmgr.h"
#include "trademgr.h"
#include "utility.h"
#include "modulemgr.h"
#include "dbevent.h"

IMPLEMENT_SINGLETON(CItemDB)

CItemDB::CItemDB()
{

}

CItemDB::~CItemDB()
{

}

bool CItemDB::Init()
{
    //LPSTR psz = "DRIVER={SQL Server};SERVER=.\\SQLEXPRESS;UID=testitem;PWD=itemdb;database=itemdb";
    //return DriverConnect(psz);
    SDBConnInfo stDBInfo = {0};
    stDBInfo.m_nSN = 0;
    sprintf(stDBInfo.m_stAccount.m_szHostName, "172.30.27.43");
    sprintf(stDBInfo.m_stAccount.m_szLoginName, "root");
    sprintf(stDBInfo.m_stAccount.m_szLoginPwd, "fyf2384381");
    sprintf(stDBInfo.m_stAccount.m_szDBName, "itemdb");
    if (!CModuleMgr::Instance()->GetDBModule()->Connect(&stDBInfo))
        return false;

    return true;
}

bool CItemDB::GetRolePKG(UINT32 dwTransID, UINT32 dwRoleID)
{
    CGetEvent* poEvent = new CGetEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, dwRoleID))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::CreateItem(UINT32 dwTransID, UINT32 dwRoleID, const ::google::protobuf::RepeatedPtrField<ItemInfo> & pstItemInfo)
{
    CCreateEvent* poEvent = new CCreateEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, dwRoleID, pstItemInfo))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::UseItem(UINT32 dwTransID, UINT32 dwRoleID, const ::google::protobuf::RepeatedPtrField<ItemBaseInfo> & pstItemsBaseInfo)
{
    CUseEvent* poEvent = new CUseEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, dwRoleID, pstItemsBaseInfo))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::ComplexOpt(UINT32 dwTransID, UINT32 dwRoleID, const ::google::protobuf::RepeatedPtrField<ItemInfo> & pstCreateItemInfo, const ::google::protobuf::RepeatedPtrField<ItemBaseInfo> & pstUseItemsBaseInfo)
{
    CComplexEvent* poEvent = new CComplexEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, dwRoleID, pstCreateItemInfo, pstUseItemsBaseInfo))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::ChangeItemPos(UINT32 dwTransID, UINT32 dwRoleID, const ::google::protobuf::RepeatedPtrField<ChangePosInfo> & pstChangeInfos)
{
    CChangeEvent* poEvent = new CChangeEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, dwRoleID, pstChangeInfos))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::TradeItem(UINT32 dwTransID, STTRADERECORD* pstRecord)
{
    CTradeEvent* poEvent = new CTradeEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, pstRecord))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::ConfirmTrade(UINT32 dwTransID, UINT32 dwRoleID, UINT32 dwTradeID)
{
    return true;
}

bool CItemDB::SendMail(UINT32 dwTransID, STMAILRECORD* pstMailRecord)
{
    CMailSendEvent* poEvent = new CMailSendEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, pstMailRecord))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::RecvMail(UINT32 dwTransID, STMAILRECORD* pstMailRecord)
{
    CMailRecvEvent* poEvent = new CMailRecvEvent();
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, pstMailRecord))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

bool CItemDB::AddQuantity(UINT32 dwTransID, UINT32 dwRoleID, const ItemBaseInfo & item, int nCount)
{
    CAddQuantityEvent * poEvent = new CAddQuantityEvent;
    if (NULL == poEvent)
        return false;

    if (!poEvent->Init(dwTransID, dwRoleID, item, nCount))
        return false;

    if (!CModuleMgr::Instance()->GetDBModule()->AddDBCommand(poEvent))
        return false;

    return true;
}

